package com.elitecore.daoImpl;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.elitecore.beans.PartnerUser;
import com.elitecore.dao.AuthenticateDAO;

@Repository 
public class AuthenticateDAOImpl implements AuthenticateDAO
{
	private String MODULE =  "AuthenticateDAOImpl";
	private static org.apache.log4j.Logger log	= Logger.getLogger(AuthenticateDAOImpl.class);

	@Autowired
	private SessionFactory sessionFactory;
	 
	public boolean isRegUser(PartnerUser userObj)
	{
		String METHOD = "isRegUser";
		boolean ans;
		
		try
		{
			log.info(" In "+METHOD+" of "+MODULE);
			log.info(" sessionFactory ===="+sessionFactory);
			
			Session session = sessionFactory.getCurrentSession();
			String hql = " From PartnerUser where login = :login_id and password = :pwd";
			Query query = session.createQuery(hql);
			query.setParameter("login_id", userObj.getLogin());
			query.setParameter("pwd", userObj.getPassword());
			
			List<PartnerUser> userInfo = (List<PartnerUser>) query.list();
			if(userInfo != null && userInfo.size() > 0)
			{
				log.info(" userInfo ===="+userInfo.get(0).getLogin());
				ans = true;
			}
			else
			{
				ans = false;
			}
		}
		catch(Exception e)
		{
			ans = false;
			log.info(" Exception in "+METHOD+" of "+MODULE+e,e);
		}
		return ans;
	}
}